package com.sdkx.wear_os.utils

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.coroutineScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch

/**
 * @author: DDY
 * @date: 2022/2/17
 * 封装FLow
 * @param owner 作用域
 * @param minActiveState 作用状态
 * @param action 回调
 */
inline fun <T> Flow<T>.launchAndCollectInx(
    owner: Lifecycle,
    minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
    crossinline action: suspend CoroutineScope.(T) -> Unit
) {
    owner.coroutineScope.launch {
        owner.repeatOnLifecycle(minActiveState) {
            collect {
                action(it)
            }
        }
    }
}
